package io.zhifei.modules.sys.pr.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import io.zhifei.common.utils.ApiResponse;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.Query;
import io.zhifei.common.utils.R;
import io.zhifei.modules.sys.pr.entity.Student;
import io.zhifei.modules.sys.pr.service.DataCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 数据采集控制器
 *
 * @author qdp
 * @Date 2021-08-09 14:40:34
 */
@Controller
@RequestMapping("/dc")
@Api(value = "数据采集控制器",description = ("数据采集控制器"))
public class DataCollectionController {

     @Autowired
     private DataCollectionService dataCollectionService;
//
//    /**
//     * 数据采集列表
//     *
//     * @author qdp
//     * @Date 2021-08-09
//     */
//    @ResponseBody
//    @GetMapping("/dcList")
//    @ApiOperation("数据采集列表(也是全量抽取)")
//    @ApiResponses({
//            @io.swagger.annotations.ApiResponse(code = 200, response = io.zhifei.modules.sys.pr.model.result.StudentResult.class, message = "成功")
//    })
//    public ApiResponse list() {
//        return this.dataCollectionService.findPageBySpec();
//    }


    /**
     * 数据采集列表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @GetMapping("/dcList")
    @ApiOperation(value = "数据采集列表(也是全量抽取)", notes = "查询分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "当前页大小", paramType = "query", required = true),
    })
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, message = "请求成功", response = Student.class)
    })
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok().put("page", new PageUtils(this.dataCollectionService.page(new Query<Student>().getPage(params), new QueryWrapper<>())));
    }

    /**
     * 增量抽取列表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @ResponseBody
    @GetMapping("/dcAddList")
    @ApiOperation("增量抽取列表")
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, response = io.zhifei.modules.sys.pr.model.result.StudentResult.class, message = "成功")
    })
    public ApiResponse dcAddList() {
        return this.dataCollectionService.dcAddList();
    }

    /**
     * 新增测试数据
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @PostMapping("/addData")
    @ResponseBody
    @ApiOperation("新增测试数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "姓名", name = "name", required = true),
            @ApiImplicitParam(value = "年龄", name = "age", required = true),
    })
    public R addItem(@RequestBody @ApiIgnore Student student) {
        this.dataCollectionService.addData(student);
        return R.ok();
    }

    /**
     * 数据抽取实时数据流(依据接口相应时间做个进度条就ok)
     */

    /**
     * 写入
     */
    @PostMapping("importCustomer")
    @ApiOperation("导入数据")
    @ResponseBody
    public ApiResponse<Object> importCustomer(MultipartFile file, MultipartFile file1) {
        try {
            Object result = dataCollectionService.importCustomer(file);
            if(null != file1)dataCollectionService.importCustomer(file1);
            return new ApiResponse<>(result);
        } catch (Exception e) {
            return new ApiResponse<>("500", "导入失败！");
        }
    }

    /**
     * 数据写入实时数据流(依据接口相应时间做个进度条就ok)
     */


    /**
     * 数据转换
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @PostMapping("/conversion")
    @ResponseBody
    @ApiOperation("数据转换")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目标字符", name = "str", required = true),
    })
    public R conversion(@ApiIgnore @RequestParam String str) {
        this.dataCollectionService.conversion(str);
        return R.ok();
    }

    /**
     * 数据剔除
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @PostMapping("/eliminate")
    @ResponseBody
    @ApiOperation("数据剔除")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id字符串", name = "ids", required = true),
    })
    public R eliminate(@ApiIgnore @RequestParam String ids) {
        this.dataCollectionService.removeByIds(StrUtil.splitTrim(ids,","));
        return R.ok();
    }

    /**
     * 数据合并copy表
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @ResponseBody
    @GetMapping("/copyList")
    @ApiOperation("copy数据列表")
    @ApiResponses({
            @io.swagger.annotations.ApiResponse(code = 200, response = io.zhifei.modules.sys.pr.model.result.StudentResult.class, message = "成功")
    })
    public ApiResponse copyList() {
        return this.dataCollectionService.copyList();
    }

    /**
     * 数据合并
     *
     * @author qdp
     * @Date 2021-08-09
     */
    @PostMapping("/merge")
    @ResponseBody
    @ApiOperation("数据合并")
    public R merge() {
        this.dataCollectionService.merge();
        return R.ok();
    }

    /**
     * 导出
     * @param request
     * @param response
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出",httpMethod = "GET")
    public void export(@ApiIgnore HttpServletRequest request, @ApiIgnore HttpServletResponse response){
        try {
            this.dataCollectionService.export(request,response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


